class Solution {
public:
    int minimumDeleteSum(string s1, string s2) {
        int m = s1.size(), n = s2.size();
        vector dp(m + 1, vector(n + 1, 0));
        
        int sum = 0;
        for(int i = 0; i < m; ++i)
        {
            sum += s1[i];
            dp[i + 1][0] = sum;
        }
        sum = 0;
        for(int j = 0; j < n; ++j)
        {
            sum += s2[j];
            dp[0][j + 1] = sum;
        }

        dp[0][0] = 0;
        for(int i = 0; i < m; ++i)
        {
            for(int j = 0; j < n; ++j)
            {
                int a = s1[i], b = s2[j];
                if(a != b)
                    dp[i + 1][j + 1] = min(dp[i + 1][j] + b, dp[i][j + 1] + a);
                else
                    dp[i + 1][j + 1] = dp[i][j];
            }
        }

        return dp[m][n];
    }
};
